package site.luojie.session.consistence.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import site.luojie.session.consistence.entity.Account;
import site.luojie.session.consistence.service.AccountService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author jie.luo
 * @description
 * @since 2020-06-30
 */
@Controller
public class LoginController {

    @Autowired
    private AccountService accountService;

    @RequestMapping("/login/toLogin")
    public String toLogin() {
        return "login";
    }

    @RequestMapping("/login")
    public String toLogin(String username, String password, HttpSession session, HttpServletRequest request) {


        Account account = accountService.findAccount(username, password);
        if (null == account) {
            request.setAttribute("errorMsg", "用户名或密码错误");
            return "login";
        } else {
            session.setAttribute("username", username);
            return "redirect:/toResult";
        }
    }
}
